package com.inossem.warehousestorage_inossem.activity.out_storage;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.out_storage.extra.OutStorageExtra;
import com.inossem.warehousestorage_inossem.adapter.common.SelectOperatorAdapter;
import com.inossem.warehousestorage_inossem.bean.common.GetOperatorBean;
import com.inossem.warehousestorage_inossem.constant.TypeEnum;
import com.inossem.warehousestorage_inossem.listener.GetOperatorListener;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.common.GetOperatorRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.OutStorageUtils;

import java.util.List;

/**
 * 作者：wsy on 2017/12/5 17:01
 * <p>
 * 邮箱：445979770@qq.com
 */

public class OutStorageAllocatingSelectOperatorActivity extends BaseActivity {
    private TextView title;

    private PullToRefreshLayout refresh;

    private PullableListView receiverList;

    private SelectOperatorAdapter adapter;

    private String conditionStr, roleCodeStr;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_receive_select_receiver);
        initView(OutStorageAllocatingSelectOperatorActivity.this);
    }

    @Override
    protected void onResumeImpl() {
        initData(OutStorageAllocatingSelectOperatorActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.receiverListRefresh);
        receiverList = (PullableListView) activity.findViewById(R.id.receiverList);
    }

    private void initData(Activity activity) {
        title.setText(activity.getResources().getString(R.string.activity_receive_select_receiver_title));
        ActivityUtils.initBackAndFunction(activity, R.string.activity_receive_add_receiver_title_short, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });

        receiverList.setPullUpFlag(false);
        receiverList.setPullDownFlag(false);
        getExtra(activity);
        getData(activity);
    }

    private void getExtra(Activity activity) {
        conditionStr = activity.getIntent().getStringExtra(OutStorageExtra.OUT_STORAGE_CONDITION);
        roleCodeStr = activity.getIntent().getStringExtra(OutStorageExtra.OUT_STORAGE_ROLE);
    }

    private void getData(Context context) {
        GetOperatorRequest.request(context, conditionStr, roleCodeStr, TypeEnum.OUT_STORAGE_ALLOCATING, new GsonBeanListener<GetOperatorBean>() {
            @Override
            public void onSuccess(GetOperatorBean bean) {
                List<GetOperatorBean.OperatorBean> allReceiverList = bean.getBody();
                List<GetOperatorBean.OperatorBean> currentReceiverList = OutStorageUtils.getOutStorageAllocatingOverviewBean(context).getBody().getUser_list();
                List<GetOperatorBean.OperatorBean> resultReceiverList = OutStorageUtils.filterReceiver(allReceiverList, currentReceiverList);
                addReceiverListData(context, resultReceiverList, receiverList);
            }
        });
    }

//    private List<OutStorageOperatorBean.DataBean.ReceiverListBean> filterReceiver(List<OutStorageOperatorBean.DataBean.ReceiverListBean> allReceiverList, List<OutStorageAllocatingOverviewBean.DataBean.OperatorListBean> currentReceiverList) {
//        if (currentReceiverList == null) {
//            return allReceiverList;
//        }
//        List<OutStorageOperatorBean.DataBean.ReceiverListBean> removeReceiverList = new ArrayList<>();
//        for (int i = 0; i < allReceiverList.size(); i++) {
//            OutStorageOperatorBean.DataBean.ReceiverListBean receiverItem = allReceiverList.get(i);
//            for (int j = 0; j < currentReceiverList.size(); j++) {
//                if (receiverItem.getId().equals(currentReceiverList.get(j).getId())) {
//                    removeReceiverList.add(receiverItem);
//                }
//            }
//        }
//        allReceiverList.removeAll(removeReceiverList);
//        return allReceiverList;
//    }


    private void addReceiverListData(Context context, List<GetOperatorBean.OperatorBean> selectReceiverList, ListView listView) {
        adapter = new SelectOperatorAdapter(context, selectReceiverList, new GetOperatorListener() {
            @Override
            public void onSelectReceiver(GetOperatorBean.OperatorBean bean) {
                OutStorageUtils.addAllocatingOperator(context, bean);
                OutStorageAllocatingSelectOperatorActivity.this.finish();
            }
        });
        receiverList.setAdapter(adapter);
    }

//    public interface ReceiveSelectReceiverListener {
//        void onSelectReceiver();
//    }
}
